<template>
  <div class="login-container">
    <!-- 表单结构 -->
    <el-form ref="myform" :rules="rules" class="myform" :model="form">
      <h3>
        <img src="../../assets/common/login-logo.png" alt="" />
      </h3>

      <el-form-item prop="mobile">
        <el-input
          v-model="form.mobile"
          prefix-icon="el-icon-user-solid"
          placeholder="username"
        ></el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input
          prefix-icon="el-icon-lock"
          show-password
          type="password"
          v-model="form.password"
          placeholder="password"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button
          :loading="loading"
          @click="login"
          class="mybtn"
          type="primary"
          >登录</el-button
        >
      </el-form-item>
      <el-form-item>
        <div class="reg">还没有账户？立即注册</div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { validateMoblie } from "@/utils/validate";
import { mapActions } from "vuex";
export default {
  data() {
    return {
      loading: false, // 登录按键加载提示
      //  表单的数据源
      form: {
        mobile: "13800000002",
        password: "123456",
      },
      // 校验规则
      rules: {
        mobile: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
          // 自定义验证
          { validator: validateMoblie },
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 4, max: 10, message: "密码长度为 4 - 10", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // 映射user/actions中的方法
    ...mapActions({
      userLogin: "user/login",
    }),
    login() {
      // 完成表单校验
      this.$refs.myform.validate(async (res) => {
        if (res) {
          this.loading = true; // 开启加载
          try {
            // 校验通过，提交数据到服务器
            // 调用vuex 中的方法
            await this.userLogin(this.form); //这里用await修饰 将异步处理变成同步运算，这样userLogin报错就会中断跳转，如果不是用await异步代码报错不会影响后面同步代码的执行，也可以用.then()

            const _redirect = this.$route.query._redirect; // 定义登录之前的路径
            if (_redirect) {
              // 登录之前存在之前的路径 则跳转到之前的路径
              this.$router.push(_redirect);
            } else {
              // 跳转到首页
              this.$router.push({ path: "/" });
              this.$message.success('欢迎')
            }
          } catch (error) {
            console.log(error.message);
          } finally {
            this.loading = false; // 关闭加载
          }
        }
      });
    },
  },
};
</script>

<style lang="scss">
.login-container {
  height: 100%;
  background-image: url("../../assets/common/login.jpg");
  background-position: center;
  .myform {
    width: 520px;
    padding: 0 35px;
    padding-bottom: 100px;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    h3 {
      margin-bottom: 40px;
    }
    .el-input__inner {
      height: 50px;
      color: #79b0fe;
      background-color: #d4e5ff;
    }
    .mybtn {
      width: 100%;
      height: 60px;
      font-size: 24px;
      background-color: #407ffe;
    }
    .reg {
      text-align: center;
      color: #fff;
    }
    .el-form-item__error {
      color: #fff;
    }
  }
}
</style>
